home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Almathera Ten Pack 1: Comms & Networking
/
Almathera Ten on Ten - Disc 1: Comms & Networking.iso
/
amiga-useful
/
perl
/
cscript
/
cscript.c
< prev
next >
Wrap
C/C++ Source or Header
|
1995-05-04
|
8KB
|
371 lines
/*********************************************************************
MODULE: cscript.c
DESCRIPTION: A program to turn a script into an executable
AUTHOR: Kent Dalton
MODIFIED:
Copyright 1991 by Kent Dalton, all rights reserved.
**********************************************************************/
/**************************** INCLUDES *******************************/
#include <stdio.h>
#include "macros.h"
#include "cprogram.h"
/**************************** CONSTANTS ******************************/
#define DEFINTERP "perl"
#define DEFINTFLAGS "-I/usr/lib/perl"
#define DEFCOMPILER "gcc"
#define DEFCFLAGS ""
#ifndef TRUE
#define TRUE 1
#define FALSE 0
#endif
#define PROGRAM "cscript"
/**************************** TYPEDEFS *******************************/
/***************************** MACROS ********************************/
#define vCHECKNEXT(index) {\
if(argv[index+1] == NULL || (argv[index+1])[0] == '-') { \
fprintf(stderr, "*** Error - option with missing argument\n"); \
exit(1); \
} \
}
/***************************** GLOBALS *******************************/
/**************************** EXTERNALS ******************************/
/*********************************************************************
FUNCTION
RETURNS:
DESCRIPTION:
SIDE EFFECTS:
**********************************************************************/
int main(argc, argv)
int argc;
char **argv;
{
char *pcBuffer=NULL, *pcTmp;
int iFormBuf=FALSE;
int iVerbose = FALSE, iFiles = 0;
int iNoCompile = FALSE;
char *pcInterp = NULL;
char *pcFile = NULL;
char *pcCFile = NULL;
char *pcHead = NULL;
char *pcOutput = NULL;
char acTmp[BUFSIZ];
int i;
/*** Functions ***/
void vCreateCFile();
char *pcUpdateString(),
*pcCreateHeader(),
*pcFormOut(),
*pcParseInterp();
/*** -- ends parsing ***/
for(i=1; i<argc; i++) {
if(!iFormBuf && (strcmp(argv[i],"--") == 0)) {
iFormBuf = TRUE;
}
else if(!iFormBuf) {
if(*argv[i] != '-') {
iFiles++;
pcFile = argv[i];
}
else {
switch(*(argv[i]+1)) {
case 'i':
vCHECKNEXT(i);
i++;
pcInterp = argv[i];
break;
case 'o':
vCHECKNEXT(i);
i++;
pcOutput = argv[i];
break;
case 'v':
iVerbose = TRUE;
break;
case 'c':
iNoCompile = TRUE;
break;
default:
sprintf(acTmp, "Unrecognized option: %s", argv[i]);
vERROR("Error", acTmp, TRUE, 1);
break;
}
}
}
else {
/*** add to buffer ***/
pcBuffer = pcUpdateString(pcBuffer, argv[i], TRUE);
}
}
if(pcFile == NULL) {
vERROR("Error", "No file specified", TRUE, 1);
}
if(iFiles > 1) {
vERROR("Error", "Too many files specified", TRUE, 1);
}
if((pcOutput=pcFormOut(pcOutput,pcFile)) == NULL) {
vERROR("Error", "Can't form output name", TRUE, 1);
}
pcInterp = pcParseInterp(pcFile,pcInterp);
pcCFile = pcUpdateString(pcCFile, pcOutput, FALSE);
pcCFile = pcUpdateString(pcCFile, ".c", FALSE);
pcHead = pcCreateHeader(pcOutput, pcFile);
vCreateCFile(pcHead, pcCFile, pcOutput, pcInterp, DEFINTFLAGS, pcBuffer);
if(iNoCompile) exit(0);
sprintf(acTmp, "%s %s -o %s %s",
DEFCOMPILER, DEFCFLAGS, pcOutput, pcCFile);
if(iVerbose)
fprintf(stderr, "%s\n", acTmp);
ssystem(acTmp);
unlink(pcHead);
unlink(pcCFile);
}
/*********************************************************************
FUNCTION: vCreateCFile
RETURNS: void
DESCRIPTION: Creates the requested C File
SIDE EFFECTS:
**********************************************************************/
void vCreateCFile(pcHead, pcCFile, pcOutput, pcInterp, pcFlags, pcArgs)
char *pcHead,
*pcCFile,
*pcOutput,
*pcInterp,
*pcFlags,
*pcArgs;
{
FILE *pFCFile;
int i;
char acTmp[BUFSIZ];
vOPENFILE(pFCFile, pcCFile, "w");
fprintf(pFCFile, "/*** C program generated by cscript ***/\n");
fprintf(pFCFile, "#include <stdio.h>\n");
fprintf(pFCFile, "#include \"%s\"\n\n\n", pcHead);
fprintf(pFCFile, "#define SCRIPT \"%s.tmp\"\n", pcOutput);
fprintf(pFCFile, "#define PROGRAM \"%s\"\n", pcOutput);
fprintf(pFCFile, "int main(argc, argv)\n int argc;\n char **argv;\n");
fprintf(pFCFile, "{\n int i;");
for(i=0; gacCProgram[i] != NULL; i++)
fprintf(pFCFile, gacCProgram[i]);
fprintf(pFCFile,
" sprintf(acTmp, \"%s %s %s %%s %%s\", SCRIPT, ((pcArgs == (char *)NULL) ? (char *)\"\": pcArgs));\n",
pcInterp,
((pcInterp == DEFINTERP) ? pcFlags : " "),
((pcArgs == NULL) ? " ":pcArgs));
fprintf(pFCFile, " ssystem(acTmp);\n");
fprintf(pFCFile, " unlink(SCRIPT);\n");
fprintf(pFCFile, "}\n");
fclose(pFCFile);
}
/*********************************************************************
FUNCTION
RETURNS:
DESCRIPTION:
SIDE EFFECTS:
**********************************************************************/
char *pcUpdateString(pcArgs, pcNew, iFlag)
char *pcArgs;
char *pcNew;
int iFlag;
{
char *pcTmp = NULL;
if(pcArgs == NULL) {
if((pcArgs = (char *)malloc(strlen(pcNew)+1)) == NULL) {
vERROR("Error", "malloc failed", TRUE, 1);
}
strcpy(pcArgs, pcNew);
}
else {
if((pcTmp = (char *)malloc(strlen(pcNew)+strlen(pcArgs)+2)) == NULL) {
vERROR("Error", "malloc failed", TRUE, 1);
}
if(iFlag) sprintf(pcTmp,"%s %s", pcArgs, pcNew);
else sprintf(pcTmp,"%s%s", pcArgs, pcNew);
free(pcArgs);
pcArgs = pcTmp;
}
return(pcArgs);
}
/*********************************************************************
FUNCTION
RETURNS:
DESCRIPTION:
SIDE EFFECTS:
**********************************************************************/
char *pcCreateHeader(pcProgram, pcScript)
char *pcProgram;
char *pcScript;
{
FILE *pFScript = NULL;
FILE *pFHeader = NULL;
char *pcHead = NULL;
char c;
char *pcUpdateString();
pcHead = pcUpdateString(pcProgram, ".h", FALSE);
vOPENFILE(pFHeader, pcHead, "w");
vOPENFILE(pFScript, pcScript, "r");
fprintf(pFHeader, "char *gacScript[] = {\n\"");
while(!feof(pFScript)) {
c=(char)fgetc(pFScript);
if(feof(pFScript)) continue;
switch(c) {
case '%':
fputc('%',pFHeader);
fputc('%',pFHeader);
break;
case '\\':
fputc('\\',pFHeader);
fputc('\\',pFHeader);
break;
case '\"':
fputc('\\',pFHeader);
fputc('\"',pFHeader);
break;
case '\n':
fputc('\\',pFHeader);
fputc('n', pFHeader);
fputc('\"',pFHeader);
fputc(',', pFHeader);
fputc('\n',pFHeader);
fputc('\"',pFHeader);
break;
default:
fputc(c,pFHeader);
}
}
fprintf(pFHeader, "\\n\",\nNULL\n};");
fclose(pFScript);
fclose(pFHeader);
return(pcHead);
}
/*********************************************************************
FUNCTION
RETURNS:
DESCRIPTION:
SIDE EFFECTS:
**********************************************************************/
char *pcFormOut(pcOut, pcFile)
char *pcOut;
char *pcFile;
{
char *pcTmp;
int i;
if(pcOut != NULL) return;
vSAVESTR(pcTmp, pcFile);
/*** Try to form file name by stripping extension ***/
for(i=strlen(pcTmp); i>0; i--) {
if(pcTmp[i] == '.') {
pcTmp[i] = '\0';
return(pcTmp);
}
}
pcTmp = pcUpdateString(pcTmp, ".exe", FALSE);
}
/*********************************************************************
FUNCTION
RETURNS:
DESCRIPTION:
SIDE EFFECTS:
**********************************************************************/
char *pcParseInterp(pcFile, pcInterp)
char *pcFile;
char *pcInterp;
{
FILE *pF;
char *pcRet;
char acStr[BUFSIZ];
int i;
if(pcInterp != NULL) return(pcInterp);
vOPENFILE(pF, pcFile, "r");
for(fgets(acStr, BUFSIZ, pF); !feof(pF); fgets(acStr, BUFSIZ, pF)) {
if((acStr[0] == '#') && (acStr[1] == '!')) {
vSAVESTR(pcRet, &(acStr[2]));
for(i=0; pcRet[i] != '\0'; i++)
if(pcRet[i] == '\n') pcRet[i] = '\0';
fclose(pF);
return(pcRet);
}
}
fclose(pF);
return(DEFINTERP);
}